package com.why.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.why.dao.mybatis.OrgDao;
import com.why.entity.EUDataGrid;
import com.why.entity.EasyUiTree;
import com.why.entity.IndexData;
import com.why.entity.Org;

@Service
public class OrgService {

	@Resource
	private OrgDao orgDao;
	
	public EasyUiTree getAllOrgs() {
		// TODO Auto-generated method stub
		List<Org> orgs = orgDao.getaAll();
		Org root = new Org();;
		for(Org i:orgs){
			String up_index = i.getUp_id();
			if(up_index != null){
				for(Org in:orgs){
					if(in.getId().equals(up_index)){
						in.setChildren(i);
						break;
					}
				}
			}else{
				root = i;
			}
		}
//		return temp_map.get("0");
		return this.transition(root);
	}
	
	private EasyUiTree transition(Org org){
		String up_index = org.getUp_id();
		EasyUiTree eut = new EasyUiTree(org.getId(),org.getOrg_name());
		eut.setUpid(up_index);
		if(org.hasChildren()){
			List<Org> children = org.getChildren();
			for(Org child:children){
				eut.addChild(transition(child));
			}
		}
		return eut;
	}
	

	private String getPath(String org_id){
		return orgDao.getOrgPathById(org_id);
	}
	
	public void getOrgById(String org_id) {
		// TODO Auto-generated method stub
		
	}

	public void getSubOrgById(String org_id) {
		// TODO Auto-generated method stub
		
	}

	public EUDataGrid getIndex_data(String org_id, String start) {
//		String s = "";
		EUDataGrid dg = new EUDataGrid();
		String path = getPath(org_id);
		List<Org> orgs = orgDao.getOrgsByPath(path);
//		str.append("{\"total\":").append(orgs.size()).append(",\"rows\":[");
		dg.setTotal(orgs.size());
		List<Map<String,Object>> rows = new ArrayList<Map<String,Object>>();
		if(orgs.size()>0){
			for(Org org:orgs){
				Map<String,Object> item = new HashMap<String,Object>();
				List<IndexData> index_datas = orgDao.getIndexData(org.getId(),start);
//				StringBuffer temp = new StringBuffer("{\"area\":").append(org.getOrg_name()).append(",");
				item.put("area", org.getOrg_name());
				item.put("id", org.getId());
				for(IndexData d : index_datas){
//					temp.append("\"").append(d.getIndex_id()).append("\":").append(d.getData_value()).append(",");
					item.put(d.getIndex_id(), d.getData_value());
				}
//				String s_temp = temp.toString();
//				s_temp =  s_temp.substring(0, s_temp.length()-1);
//				s_temp = s_temp+"},";
//				str.append(s_temp);
				rows.add(item);
			}
		}
		dg.setRows(rows);
		return dg;
	}

	public List<List<Object>> getExport_data(String org_id, String start, String end,List<String> title) {
		List<List<Object>> list = new ArrayList<List<Object>>();
		if(end == null || end.equals("")){
			String path = getPath(org_id);
			List<Org> orgs = orgDao.getOrgsByPath(path);
			if(orgs.size()>0){
				for(Org org:orgs){
					List<Object> o_list = new ArrayList<Object>();
					o_list.add(org);
					List<IndexData> index_datas = orgDao.getIndexData(org.getId(),start);
					Map<String,IndexData> d_map = new HashMap<String,IndexData>();
					for(IndexData index:index_datas){
						d_map.put(index.getIndex_id(), index);
					}
					for(String t:title){
						if(t.equals("area")){
							continue;
						}
						IndexData temp = d_map.get(t);
						if(temp == null){
							IndexData t_o = new IndexData();
							t_o.setIndex_id(t);
							o_list.add(t_o);
						}else{
							o_list.add(temp);
						}
					}
					list.add(o_list);
				}
			}
		}else{
			//年份
			List<Org> orgs = orgDao.getDataById(org_id);
		}
		return list;
	}
}
